package fdoj.eventInvitations.entity;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.FetchType.EAGER;
import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "contacts")
public class Contact implements Serializable {
	/**
	 * ova klasa implementira kontakt iz naseg model podataka
	 */
	private static final long serialVersionUID = 7939600919940204168L;
	/**
	 * @param id je identifikator
	 */
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "contact_id", unique = true, nullable = false)
	private Integer id;
	/**
	 * @param valid 
	 */
	@Column(name = "valid", unique = false, nullable = false)
	private Boolean valid;
	/**
	 * @param firstName je ime
	 */
	@Column(name = "firstName", unique = false, nullable = true)
	private String firstName;
	/**
	 * @param lasteName prezime
	 */
	@Column(name = "lastName", unique = false, nullable = true)
	private String lastName;
	/**
	 * @param email je mejl
	 */
	@Column(name = "email", unique = false, nullable = false)
	private String email;

	@OneToMany(cascade = { ALL }, fetch = EAGER, mappedBy = "eventCall")
	private Set<Invantation> invites = new HashSet<Invantation>();

	@ManyToOne
	@JoinColumn(name = "addressBook_id", referencedColumnName = "person_id", nullable = false)
	private Person addressBook;
	

	public void addInvites(Invantation invantation) {
		if (invantation.getEventCall() != null)
			invantation.getEventCall().getInvites().remove(invantation);
		invantation.setEventCall(this);
		getInvites().add(invantation);
	}
	
	public void removeInvite(Invantation invitation){
		invitation.setEventCall(null);
		invites.remove(invitation);
	}


	public Contact() {

	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Person getAddressBook() {
		return addressBook;
	}

	public void setAddressBook(Person addressBook) {
		this.addressBook = addressBook;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getLastName() {
		return lastName;
	}

	public Set<Invantation> getInvites() {
		return invites;
	}

	public void setInvites(Set<Invantation> invites) {
		this.invites = invites;
	}

	public Boolean isValid() {
		return valid;
	}

	public void setValid(Boolean valid) {
		this.valid = valid;
	}

}
